//
//  HKNetService+Config.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/11/13.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKNetService+Config.h"
#import "HKPrefixHeader.h"
#import "HKIdentify.h"
#import "AFNetworking.h"
//#import "XBBaseViewController.h"
@implementation HKNetService (Config)
- (void)configHandle {
    //HKWeakSelf
    self.abnormalBlock = ^(HKResultMessage *resultMessage) {
        
        if (resultMessage.result==HKServiceToken_is_invalite || resultMessage.result == HKServiceUserLoginDisable || resultMessage.result == HKServiceIllegalUserId || resultMessage.result==HKServiceNonePermit) {
            //                [XBTool removeToken];
            //                [[XBIdentify sharedInstance] setUserState:NO];
            //                [[XBIdentify sharedInstance] logout];
            //                [HKNotiCenter postNotificationName:@"noAuthLogin" object:nil];
        }
        
        if (resultMessage.result == HKServiceToken_is_invalite) {
            [resultMessage setMessage:@"您的账号在另一台设备被登录"];
        }
        
        if (resultMessage.result == HKServiceUserLoginDisable) {
            [resultMessage setMessage:@"账号被禁用，请联系客服。"];
            dispatch_async(dispatch_get_main_queue(), ^{
                //禁用后跳转登录页
                //                    [(XBBaseViewController*)[XBTool getCurrentVC] miguLogin];
                //                    [XBTool removeToken];
                //                    [[XBIdentify sharedInstance] setUserState:NO];
                //                    [[XBIdentify sharedInstance] logout];
            });
        }
    };
    
    
    self.requestHeaderBlock = ^(AFHTTPSessionManager *manager) {
        //HKStrongSelf
        //Cookie
        HKIdentify *identify = [HKIdentify sharedInstance];
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"requestCookie"]) {
            [manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"requestCookie"] forHTTPHeaderField:@"Cookie"];
        }
        //token
        NSString *token = [identify getToken];
        [manager.requestSerializer setValue:token.length ? token : @"" forHTTPHeaderField:@"Authorization"];
        
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        //        if (self.useHttps) {
        //2.设置非校验证书模式
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        [manager.securityPolicy setValidatesDomainName:NO];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/JavaScript",@"text/html",@"text/plain", nil]];
        //        }
        
        /*
         ios:
         channel:3
         accessCode:161107015550
         accessCodeKey（密钥）:3A4B04_6C7@F1D01
         */
        //添加咪咕加密请求头
        NSString *sessionId = MyUUID;
        NSString *timeStamp = [NSString miGuTimeStamp];
        NSString *accessCode = @"161107015550";//161107015550 161107016888
        NSString *channel = @"3";
        NSString *accessCodeKey = @"3A4B04_6C7@F1D01";//秘钥 secretKey="283@2B2F199_1CF1" @"3A4B04_6C7@F1D01"
        NSString * mdn = [HKUserDefaults objectForKey:@"phoneNum"];
        if (mdn.length>0) {
            mdn = [HKUserDefaults objectForKey:@"phoneNum"];
        }else {
            mdn = @"15959285442";
        }
        NSString *md5PhoneNum = [mdn base64EncodeString];//Mid手机号码，base64编码
        NSString * signature  = [NSString stringWithFormat:@"%@%@%@%@%@%@",sessionId,timeStamp,channel,accessCode,accessCodeKey,md5PhoneNum];//@"Signature"
        NSString * md5String = [signature md532UpperString];
        //[_sessionManager.requestSerializer setValue:@"text/plain;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:@"1.0" forHTTPHeaderField:@"Version"];
        [manager.requestSerializer setValue:sessionId forHTTPHeaderField:@"SessionId"];
        [manager.requestSerializer setValue:timeStamp forHTTPHeaderField:@"TimeStamp"];
        [manager.requestSerializer setValue:md5String forHTTPHeaderField:@"Signature"];
        [manager.requestSerializer setValue:channel forHTTPHeaderField:@"Channel"];
        [manager.requestSerializer setValue:accessCode forHTTPHeaderField:@"AccessCode"];
        [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"NetStandard"];
        [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"NetAccessType"];
        [manager.requestSerializer setValue:md5PhoneNum forHTTPHeaderField:@"Mid"];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/JavaScript",@"text/html",@"text/plain", nil]];
    };
}
@end
